﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;

namespace filmquiz.Src.Ftp
{
    public class FtpHelper
    {

        private static FtpHelper instance = null;

        private FtpHelper()
        {
        }

        public static FtpHelper getInstance()
        {
            if (instance == null)
            {
                instance = new FtpHelper();
            }
            return instance;
        }

        public string UploadFile(string filename, int round)
        {
            FileInfo fileInf = new FileInfo(filename);
            FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(
                  Statics.getInstance().getFtpUrl() + "/" + round + "/" + fileInf.Name));

            reqFTP.Credentials = new NetworkCredential(Statics.getInstance().getFtpUserName(), Statics.getInstance().getFtpPassword());

            reqFTP.KeepAlive = false;

            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            reqFTP.UseBinary = true;

            reqFTP.ContentLength = fileInf.Length;

            const int buffLength = 2048;
            byte[] buff = new byte[buffLength];

            FileStream fs = fileInf.OpenRead();
            try
            {
                Stream strm = reqFTP.GetRequestStream();
                int contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                return ex.StackTrace;
            }
            return "Fil uploadet";
        }

        public List<string> namesOfImagesOnFtp(int round)
        {
            string url = Statics.getInstance().getFtpUrl() + "/" + round + "/";
            FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(url);

            reqFTP.Credentials = new NetworkCredential(Statics.getInstance().getFtpUserName(), Statics.getInstance().getFtpPassword());

            reqFTP.KeepAlive = false;

            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

            WebResponse response = reqFTP.GetResponse();

            StreamReader reader = new StreamReader(response.GetResponseStream());

            string lsdirectory = reader.ReadLine();
            List<string> lsnames = new List<string>();

            while (lsdirectory != null)
            {
                lsnames.Add(lsdirectory);
                lsdirectory = reader.ReadLine();
            }

            return lsnames;
        }

        public int getNumberOfImages(int round)
        {
            int count = namesOfImagesOnFtp(round).Count;
            return count;
        }

        public void deleteFileFromFtp(int round, int image)
        {
            string url = Statics.getInstance().getFtpUrl() + "/" + round + "/" + image + ".jpg";
            FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(url);

            reqFTP.Credentials = new NetworkCredential(Statics.getInstance().getFtpUserName(), Statics.getInstance().getFtpPassword());

            reqFTP.KeepAlive = false;

            reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;

            reqFTP.GetResponse().Close();
        }
    }
}